package com.web.exiaodao.business.controller;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.web.exiaodao.business.entity.CheckInTable;
import com.web.exiaodao.business.service.CheckInTableService;
import com.web.exiaodao.remote.vo.CheckInTableVo;

/**
 * @Title: CheckInTableController.java
 * @Package com.web.exiaodao.business.controller
 * @Description: TODO
 * @author wangxuemei  
 * @date 2016-12-13 上午10:13:42
 * @version V1.0
 */
@Controller
@RequestMapping("/cms/checkInTable")
public class CheckInTableController extends BaseController {

	private static final Logger LOGGER = Logger.getLogger(CheckInTableController.class);

	@Autowired
	private CheckInTableService checkInTableService;
	
	/**
	 * @Title: findCheckInTableList
	 * @Description: 分页查询CheckInTable	 * @param nextPage
	 * @param pageSize
	 * @param status		//默认查询非删除3的数据,具体类型参照XaConstant.Status
	 * @param sortData
	 * @param jsonFilter
	 * @return    
	 */
	@ResponseBody
	@RequestMapping(value="findCheckInTableList",method=RequestMethod.POST)
	public XaResult<Page<CheckInTableVo>> findCheckInTableList(
		@RequestParam(defaultValue = "0") Integer nextPage,
		@RequestParam(defaultValue = "10") Integer pageSize,
		@RequestParam(defaultValue = "3") Integer status,
		@RequestParam(defaultValue = "{}") String jsonFilter
	) throws BusinessException{
		XaResult<Page<CheckInTableVo>> xr = new XaResult<Page<CheckInTableVo>>();
		try{
			Map<String,Object> filterParams =  WebUitl.getParametersStartingWith(jsonFilter, "search_");
			return checkInTableService.findCheckInTableList(nextPage, pageSize, filterParams);
		}catch(Exception e){
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
			xr.error(XaConstant.Message.error);
		}
		return xr;
	}
	
	/**
	 * @Title: findCheckInTableEQStatusPage
	 * @Description: (预留)分页查询CheckInTable	 * @param nextPage
	 * @param pageSize
	 * @param status		//默认查询正常状态1的数据,具体类型参照XaConstant.Status
	 * @param sortData
	 * @param jsonFilter
	 * @return    
	 */
	@ResponseBody
	@RequestMapping(value="findCheckInTableEQStatusPage",method=RequestMethod.POST)
	public XaResult<Page<CheckInTable>> findCheckInTableEQStatusPage(
		@RequestParam(defaultValue = "0") Integer nextPage,
		@RequestParam(defaultValue = "10") Integer pageSize,
		@RequestParam(defaultValue = "1") Integer status,
		@RequestParam(defaultValue = "[{property:'createTime',direction:'DESC'}]") String sortData,
		@RequestParam(defaultValue = "{}") String jsonFilter
	) throws BusinessException{
		XaResult<Page<CheckInTable>> xr = new XaResult<Page<CheckInTable>>();
		try{
			Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, sortData);
			Map<String,Object> filterParams =  WebUitl.getParametersStartingWith(jsonFilter, "search_");
			return checkInTableService.findListEQStatusByFilter(status, filterParams, pageable);
		}catch(Exception e){
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
			xr.error(XaConstant.Message.error);
		}
		return xr;
	}
	
	/**
	 * @Title: findCheckInTableById
	 * @Description: 根据ID查找单条实体
	 * @param modelId
	 * @return    
	 */
	@ResponseBody
	@RequestMapping(value="findCheckInTableById",method=RequestMethod.POST)
	public XaResult<CheckInTable> findCheckInTableById(
		@RequestParam Long modelId
	) throws BusinessException{
		XaResult<CheckInTable> xr = new XaResult<CheckInTable>();
		try{
			return checkInTableService.findOne(modelId);
		}catch(Exception e){
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
			xr.error(XaConstant.Message.error);
		}
		return xr;
	}
	/**
	 * @Title: saveModel
	 * @Description: 保存实体数据
	 * @param model
	 * @return    
	 */
	@ResponseBody
	@RequestMapping(value="saveCheckInTable",method=RequestMethod.POST)
	public XaResult<CheckInTable> saveCheckInTable(
		CheckInTable model
	) throws BusinessException{
		XaResult<CheckInTable> xr = new XaResult<CheckInTable>();
		try{
			return checkInTableService.saveOrUpdate(model);
		}catch(Exception e){
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
			xr.error(XaConstant.Message.error);
		}
		return xr;
	}
	
	/**
	 * @Title: multiOperateCheckInTableByIds
	 * @Description: 批量操作多个实体状态,根据status进行操作
	 * @param modelIds    	多个实体id,中间用逗号隔开
	 * @param status 		操作类型,status类型见XaConstant.Status,默认删除操作
	 * @return    
	 */
	@ResponseBody
	@RequestMapping(value="operateCheckInTableByIds",method=RequestMethod.POST)
	public XaResult<CheckInTable> operateCheckInTableByIds(
		HttpServletRequest request,
		@RequestParam String modelIds,
		@RequestParam(defaultValue = "3") Integer status
	) throws BusinessException{
		XaResult<CheckInTable> xr = new XaResult<CheckInTable>();
		try{
			return checkInTableService.multiOperate(modelIds,status);
		}catch(Exception e){
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
			xr.error(XaConstant.Message.error);
		}
		return xr;
	}
	
	/**
	 * @Title: upload
	 * @Description: 图片上传
	 * @param photoFile
	 * @param request
	 * @return    
	 */
	@RequestMapping(value="photoUpload",method=RequestMethod.POST)
	public void photoUpload(
		@RequestParam(value = "photoFile", required = false) MultipartFile photoFile, 
		HttpServletRequest request,
		HttpServletResponse response
	) throws BusinessException{
		String result = "";
		String root=request.getSession().getServletContext().getRealPath("/");
		String picturePath = "/upload/checkInTable";
		String ext =photoFile.getOriginalFilename().substring(photoFile.getOriginalFilename().lastIndexOf("."));
		String newName = new Date().getTime()+ext;
		File filedict = new File(root+picturePath);
		if(!filedict.exists()){
			filedict.mkdir();
		}
		File targetFile=new File(root+picturePath+File.separator+newName);
		try {
			if(StringUtils.equalsIgnoreCase(ext, ".jpg") || StringUtils.equalsIgnoreCase(ext, ".png")){
				photoFile.transferTo(targetFile);
				BufferedImage bimg = ImageIO.read(targetFile);
				int width = bimg.getWidth();
				int height = bimg.getHeight();
				result = picturePath + "/" + newName + "," + width + "," + height;
				response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print(JSON.toJSONString(result));
			}else{
				LOGGER.info("上传文件类型不允许,请上传jpg/png图片");
				throw new BusinessException("上传文件类型不允许,请上传jpg/png图片");
			}
		} catch (IllegalStateException e) {
			throw new BusinessException("图片上传失败");
		} catch (IOException e) {
			throw new BusinessException("图片上传失败");
		} catch (Exception e) {
			throw new BusinessException("图片上传失败");
		}
	}
	
}

